package com.hc.programming.util;

import java.lang.reflect.Array;
import java.util.Arrays;

/**
 * @author huangchao E-mail:fengquan8866@163.com
 * @version 创建时间：2024/7/11 18:09
 */
public class ArrayUtil {
    public static void println(int[][] arrs) {
        if (arrs == null) {
            return;
        }
        for (int[] arr : arrs) {
            System.out.println(Arrays.toString(arr));
        }
    }

    /**
     * 打印多级对象
     *
     * @param arrs
     */
    public static void print(Object arrs) {
        System.out.print(toString(arrs));
    }

    /**
     * 打印多级对象
     *
     * @param arrs
     */
    public static void println(Object arrs) {
        System.out.println(toString(arrs));
    }

    public static String toString(Object arrs) {
        if (arrs == null) {
            return null;
        }
        if (!isArray(arrs)) {
            return String.valueOf(arrs);
        }
        int length = Array.getLength(arrs);
        StringBuilder sb = new StringBuilder("[");
        for (int i = 0; i < length; i++) {
            Object item = Array.get(arrs, i);
            if (isArray(item)) {
                sb.append(toString(item));
            } else {
                sb.append(item);
            }
            if (i != length - 1) {
                sb.append(", ");
            }
        }
        sb.append(']');
        return sb.toString();
    }

    public static boolean isArray(Object obj) {
        return obj != null && obj.getClass().isArray();
    }

    public static void main(String[] args) {
        println(new int[]{1, 2, 3});
        println(new int[][]{{1, 2, 3}, {1, 2, 3}, {1, 2, 3}});
        System.out.println(toString(new int[][]{{1, 2, 3}, {1, 2, 3}, {1, 2, 3}}));
    }
}
